<template>
	<view class="container">
		<view class="hotel-item" @click="selectHotelHanlde(item)" v-for="(item,index) in hotlList">
			{{item.name}}
		</view>


		<view class="hotel-item" style="background: #f1f1f1;color: #000;" @click="outHandle">
			退出
		</view>
	</view>
</template>

<script>
	import {
		hotelList,
		switchHotel
	} from '@/api/hotel.js'
	import {
		loginout
	} from '@/api/login.js'
	export default {
		data() {
			return {
				hotlList: []
			};
		},
		onShow() {
			this.getHotelList()
		},
		methods: {
			// 获取酒店列表
			async getHotelList() {
				const res = await hotelList()
				this.hotlList = res.data
			},
			async loginoutHandle() {
				const res = await loginout()
				console.log(res)
				if(res.code==0){
					// 移除本地存储的 token
					uni.removeStorageSync('token');
					uni.removeStorageSync('name');
					// 跳转到登录页面
					uni.reLaunch({
						url: '/pages/login/login'
					});
				}
			},
			//点击退出
			async outHandle() {
				let that = this
				uni.showModal({
					content: "是否确认退出登录?",
					showCancel: true,
					success: async (result) => {
						// 如果用户点击了确定按钮
						if (result.confirm) {
							try {
								// 等待 loginout 函数执行完毕
								that.loginoutHandle()

							} catch (error) {
								// 处理错误
								console.error('退出登录失败', error);
							}
						}
					}
				});
			},
			// 选择酒店
			async selectHotelHanlde(item) {
				const res = await switchHotel({
					hotelId: item.id
				})
				uni.setStorageSync('token', res.data.token)
				uni.setStorageSync('name', item.name)
				console.log(uni.getStorageSync('name'))
				uni.switchTab({
					url: '/pages/index/index'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		background-color: #FFF;
		min-height: 100vh;
		padding: 20rpx;
		box-sizing: border-box;
	}

	.hotel-item {
		width: 710rpx;
		height: 152rpx;
		background: #066AFB;
		border-radius: 8rpx;
		font-family: PingFang SC;
		font-weight: bold;
		font-size: 34rpx;
		color: #FFFFFF;
		line-height: 152rpx;
		text-align: center;
	}

	.hotel-item:nth-child(n+1) {
		margin-top: 20rpx;
	}
</style>